<?php

namespace app\admin\model;

use app\common\model\Category;
use app\common\model\Product;
use think\Model;


class Dressup extends Model
{

    

    

    // 表名
    protected $name = 'dressup';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_list_text',
        'is_free_text',
        'status_text',
        'createtime_text',
        'updatetime_text'
    ];


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypelistList()
    {
        return ['virtual' => __('Typelist virtual'), 'physical' => __('Typelist physical')];
    }


    public function getTypeListTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type_list']) ? $data['type_list'] : '');
        $list = $this->getTypelistList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsFreeList()
    {
        return ['Y' => __('IsFree Y'), 'N' => __('IsFree N')];
    }


    public function getIsFreeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_free']) ? $data['is_free'] : '');
        $list = $this->getIsFreeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

//    public function packageds()
//    {
//        return $this->belongsToMany(Packaged::class,'\app\admin\model\Dressuppackaged','ids');
//    }
}
